﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.MIddle_341_NestedIterator
{
    class MainProject
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(TimeRequiredToBuy(new int[] { 84, 49, 5, 24, 70, 77, 87, 8 }, 3));
            Console.ReadKey();
        }
    }

    public interface NestedInteger
    {

        // @return true if this NestedInteger holds a single integer, rather than a nested list.
        bool IsInteger();

        // @return the single integer that this NestedInteger holds, if it holds a single integer
        // Return null if this NestedInteger holds a nested list
        int GetInteger();

        // @return the nested list that this NestedInteger holds, if it holds a nested list
        // Return null if this NestedInteger holds a single integer
        IList<NestedInteger> GetList();
    }

    public class NestedIterator
    {
        private Queue<int> m_Que = new Queue<int>();

        public NestedIterator(IList<NestedInteger> nestedList)
        {
            GetNumber(nestedList, m_Que);
        }

        private void GetNumber(IList<NestedInteger> nestedList, Queue<int> que)
        {
            foreach (var item in nestedList)
            {
                if (item.IsInteger()) que.Enqueue(item.GetInteger());
                else GetNumber(item.GetList(), que);
            }
        }

        public bool HasNext()
        {
            return m_Que.Count > 0 ? true : false;
        }

        public int Next()
        {
            return m_Que.Dequeue();
        }
    }
}
